package com.infore.qualityControl.export.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.infore.qualityControl.bo.QualityParamAnalysisBo;
import com.infore.qualityControl.bo.QualityParamSelectBo;
import com.infore.qualityControl.enums.ExportTableEnum;
import com.infore.qualityControl.export.policy.bo.ExportParamBo;
import com.infore.qualityControl.export.policy.handler.ExportHandler;
import com.infore.qualityControl.export.service.IExportService;
import com.infore.qualityControl.export.util.ExportUtil;
import com.infore.qualityControl.po.FiveParameter;
import com.infore.qualityControl.po.Recovery;
import com.infore.qualityControl.po.VerificationOfStandardSample;
import com.infore.qualityControl.remoteEntity.EquipmentInfo;
import com.infore.qualityControl.remoteService.IDataReceivingService;
import com.infore.qualityControl.select.util.PageObject;
import com.infore.qualityControl.service.ICQTestDataService;
import com.infore.qualityControl.service.IQualityAnalysisService;
import com.infore.qualityControl.service.ITestDataService;
import com.infore.qualityControl.util.MyBeanUtils;
import com.infore.qualityControl.util.ObjectMapperUtil;
import com.infore.qualityControl.util.ResultEntity;


@Service
public class ExportServiceImpl implements IExportService {

	@Autowired
	ICQTestDataService ICQTestDataService;

	@Autowired
	ITestDataService ITestDataService;
	
	@Autowired
	IQualityAnalysisService IQualityAnalysisService;

	@Autowired
	IDataReceivingService IDataReceivingService;

	@Autowired
	ObjectMapperUtil ObjectMapperUtil;
	
	@Autowired
	ExportHandler ExportHandler;
	
	@Override
	public byte[] exportTestDataExcelData(HttpServletRequest request, String flag) throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		// 查询数据的参数
		String startTime = paramsJson.getString("startTime");
		String endTime = paramsJson.getString("endTime");
		String indexCodes = paramsJson.getString("indexCodes");
		String stationCodes = paramsJson.getString("stationCodes");
		Boolean ifCurrentSection = paramsJson.getBoolean("ifCurrentSection");
		String pattern = paramsJson.getString("pattern"); // 时间类型
		// 表格类型
		String tableType = paramsJson.getString("tableType");
		// 日志类型
		String logType = paramsJson.getString("logType");
		// 质控分析类型
		String qualityAnalysisType = paramsJson.getString("qualityAnalysisType");
		// 测试数据模块下的参数对象
		QualityParamSelectBo qualityParamSelectBo = new QualityParamSelectBo();
		qualityParamSelectBo.setStartTime(startTime);
		qualityParamSelectBo.setEndTime(endTime);
		qualityParamSelectBo.setIndexCodes(indexCodes);
		qualityParamSelectBo.setStationCodes(stationCodes);
		qualityParamSelectBo.setIfCurrentSection(ifCurrentSection);
		// 质控分析模块下的参数对象
		QualityParamAnalysisBo qualityParamAnalysisBo = new QualityParamAnalysisBo();
		qualityParamAnalysisBo.setStartTime(startTime);
		qualityParamAnalysisBo.setEndTime(endTime);
		qualityParamAnalysisBo.setStationCodes(stationCodes);
		qualityParamAnalysisBo.setQualityAnalysisType(qualityAnalysisType);
		PageObject pageObject = new PageObject();
		pageObject.setPageNumber(null);
		pageObject.setPageSize(null);
		ExportParamBo exportParamBo = new ExportParamBo(qualityParamSelectBo, qualityParamAnalysisBo, pageObject, request, headerProp, pattern,
				tableType, ifCurrentSection, stationCodes, startTime, endTime, logType, flag);
		return ExportHandler.getResource(tableType, exportParamBo);
//		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//		if (tableType.equals(ExportTableEnum.ZERO_DRIFT_DATA.getCode())) { // 零点漂移
//			ResultEntity resultEntity = ITestDataService.zeroDriftData(qualityParamSelectBo, pageObject);
//			list = ObjectMapperUtil.convertListMap(resultEntity);
//		} else if (tableType.equals(ExportTableEnum.SPAN_DRIFT_DATA.getCode())) { // 跨度漂移
//			ResultEntity resultEntity = ITestDataService.spanDriftData(qualityParamSelectBo, pageObject);
//			list = ObjectMapperUtil.convertListMap(resultEntity);
//		} else if (tableType.equals(ExportTableEnum.FIVE_PARAMETER_DATA.getCode())) { // 常规五参数
//			ResultEntity resultEntity = ITestDataService.fiveParameterData(qualityParamSelectBo, pageObject);
//			List<FiveParameter> fiveParametersList = ObjectMapperUtil.convertList(resultEntity, FiveParameter.class);
//			// 如果返回的是集合实体对象,则需要转换为map
//			for (FiveParameter fiveParameter : fiveParametersList) {
//				list.add(MyBeanUtils.convertBeanToMap(fiveParameter, pattern));
//			}
//		} else if (tableType.equals(ExportTableEnum.MULTIPOINT_LINEARITY_DATA.getCode())) { // 多点线性
//			ResultEntity resultEntity = ITestDataService.multipointlinearityData(qualityParamSelectBo, pageObject);
//			List<MultipointLinearity> multipointLinearityList = ObjectMapperUtil.convertList(resultEntity, MultipointLinearity.class);
//			for (MultipointLinearity multipointLinearity : multipointLinearityList) {
//				list.add(MyBeanUtils.convertBeanToMap(multipointLinearity, pattern));
//			}
//		} else if (tableType.equals(ExportTableEnum.REAL_WATER_DATA.getCode())) { // 实际水样
//			ResultEntity resultEntity = ITestDataService.realWaterSampleComparisonData(qualityParamSelectBo, pageObject);
//			if (ifCurrentSection) { // 当前断面走特殊表格导出接口
//				return ExportUtil.exportSpecialExcel(request, headerProp, pattern, tableType, resultEntity);
//			} else {
//				list = ObjectMapperUtil.convertListMap(resultEntity);
//			}
//		} else if (tableType.equals(ExportTableEnum.INTEGRATED_INTERVENTION_DATA.getCode())) { // 集成干预
//			ResultEntity resultEntity = ITestDataService.integratedInterventionData(qualityParamSelectBo, pageObject);
//			return ExportUtil.exportSpecialExcel(request, headerProp, pattern, tableType, resultEntity);
//		} else if (tableType.equals(ExportTableEnum.RECOVERY_DATA.getCode())) { // 加标回收
//			ResultEntity resultEntity = ITestDataService.recoveryData(qualityParamSelectBo, pageObject);
//			List<Recovery> recoveryList = ObjectMapperUtil.convertList(resultEntity, Recovery.class);
//			for (Recovery recovery : recoveryList) {
//				list.add(MyBeanUtils.convertBeanToMap(recovery, pattern));
//			}
//		} else if (tableType.equals(ExportTableEnum.VERIFICATION_STANDARD_SAMPLES_DATA.getCode())) { // 标样核查
//			ResultEntity resultEntity = ITestDataService.verificationOfStandardSamplesData(qualityParamSelectBo,
//					pageObject);
//			List<VerificationOfStandardSample> verifList = ObjectMapperUtil.convertList(resultEntity,
//					VerificationOfStandardSample.class);
//			for (VerificationOfStandardSample verificationOfStandardSample : verifList) {
//				list.add(MyBeanUtils.convertBeanToMap(verificationOfStandardSample, pattern));
//			}
//		} else if (tableType.equals(ExportTableEnum.LOG_STATUS_DATA.getCode())) { // 日志及状态量参数
//			ResultEntity resultEntity = IDataReceivingService.queryLogStatusData(stationCodes, startTime, endTime,
//					logType, null, null);
//			List<EquipmentInfo> equipmentInfoList = ObjectMapperUtil.convertList(resultEntity, EquipmentInfo.class);
//			for (EquipmentInfo equipmentInfo : equipmentInfoList) {
//				list.add(MyBeanUtils.convertBeanToMap(equipmentInfo, pattern));
//			}
//		}
//		return ExportUtil.exportExcel(request, list, headerProp, pattern);
	}
	
	@Override
	public byte[] exportQualityAnalysisTableData(HttpServletRequest request) throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		// 查询数据的参数
		String startTime = paramsJson.getString("startTime");
		String endTime = paramsJson.getString("endTime");
		String stationCodes = paramsJson.getString("stationCodes");
		String qualityAnalysisType = paramsJson.getString("qualityAnalysisType");
		String pattern = paramsJson.getString("pattern"); // 时间类型
		// 表格类型
		String tableType = paramsJson.getString("tableType");
		QualityParamAnalysisBo qualityParamAnalysisBo = new QualityParamAnalysisBo();
		qualityParamAnalysisBo.setStartTime(startTime);
		qualityParamAnalysisBo.setEndTime(endTime);
		qualityParamAnalysisBo.setStationCodes(stationCodes);
		qualityParamAnalysisBo.setQualityAnalysisType(qualityAnalysisType);
		PageObject pageObject = new PageObject();
		pageObject.setPageNumber(null);
		pageObject.setPageSize(null);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (tableType.equals(ExportTableEnum.QUALITY_CONTROLLER_EXAMINE.getCode())) { // 质控考核分析
			ResultEntity resultEntity = IQualityAnalysisService.qualityControllerExamineData(qualityParamAnalysisBo, pageObject);
			list = ObjectMapperUtil.convertListMap(resultEntity);
		} else if (tableType.equals(ExportTableEnum.DATA_VALIDITY.getCode())) { // 数据有效性分析
			ResultEntity resultEntity = IQualityAnalysisService.effectivenessData(qualityParamAnalysisBo, pageObject);
			list = ObjectMapperUtil.convertListMap(resultEntity);
		} else if (tableType.equals(ExportTableEnum.MONTH_REPORT.getCode())) { // 月度报表台账
			ResultEntity resultEntity = IQualityAnalysisService.monthReportStandingBook(qualityParamAnalysisBo, pageObject);
			list = ObjectMapperUtil.convertListMap(resultEntity);
		}
		return ExportUtil.exportExcel(request, list, headerProp, pattern);
	}

	@Override
	public byte[] exportCQTestDataExcelData(HttpServletRequest request) throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		// 查询数据的参数
		String startTime = paramsJson.getString("startTime");
		String endTime = paramsJson.getString("endTime");
		String indexCodes = paramsJson.getString("indexCodes");
		String stationCodes = paramsJson.getString("stationCodes");
		Boolean ifCurrentSection = paramsJson.getBoolean("ifCurrentSection");
		String pattern = paramsJson.getString("pattern"); // 时间类型
		// 表格类型
		String tableType = paramsJson.getString("tableType");
		QualityParamSelectBo qualityParamSelectBo = new QualityParamSelectBo();
		qualityParamSelectBo.setStartTime(startTime);
		qualityParamSelectBo.setEndTime(endTime);
		qualityParamSelectBo.setIndexCodes(indexCodes);
		qualityParamSelectBo.setStationCodes(stationCodes);
		qualityParamSelectBo.setIfCurrentSection(ifCurrentSection);
		PageObject pageObject = new PageObject();
		pageObject.setPageNumber(null);
		pageObject.setPageSize(null);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (tableType.equals(ExportTableEnum.ZERO_SPAN_CHECK_DATA.getCode())) {
			ResultEntity resultEntity = ICQTestDataService.zeroSpanCheckData(qualityParamSelectBo, pageObject);
			list = ObjectMapperUtil.convertListMap(resultEntity);
		} else if (tableType.equals(ExportTableEnum.ZERO_SPAN_DRIFT_DATA.getCode())) {
			ResultEntity resultEntity = ICQTestDataService.zeroSpanDriftData(qualityParamSelectBo, pageObject);
			list = ObjectMapperUtil.convertListMap(resultEntity);
		} else if (tableType.equals(ExportTableEnum.FIVE_PARAMETER_DATA.getCode())) {
			ResultEntity resultEntity = ICQTestDataService.fiveParameterData(qualityParamSelectBo, pageObject);
			List<FiveParameter> fiveParametersList = ObjectMapperUtil.convertList(resultEntity, FiveParameter.class);
			for (FiveParameter fiveParameter : fiveParametersList) {
				list.add(MyBeanUtils.convertBeanToMap(fiveParameter, pattern));
			}
		} else if (tableType.equals(ExportTableEnum.RECOVERY_DATA.getCode())) {
			ResultEntity resultEntity = ITestDataService.recoveryData(qualityParamSelectBo, pageObject);
			// 如果返回的是集合实体对象,则需要转换为map
			List<Recovery> recoveryList = ObjectMapperUtil.convertList(resultEntity, Recovery.class);
			for (Recovery recovery : recoveryList) {
				list.add(MyBeanUtils.convertBeanToMap(recovery, pattern));
			}
		} else if (tableType.equals(ExportTableEnum.VERIFICATION_STANDARD_SAMPLES_DATA.getCode())) {
			ResultEntity resultEntity = ITestDataService.verificationOfStandardSamplesData(qualityParamSelectBo,
					pageObject);
			List<VerificationOfStandardSample> verifList = ObjectMapperUtil.convertList(resultEntity,
					VerificationOfStandardSample.class);
			for (VerificationOfStandardSample verificationOfStandardSample : verifList) {
				list.add(MyBeanUtils.convertBeanToMap(verificationOfStandardSample, pattern));
			}
		} else if (tableType.equals(ExportTableEnum.LOG_STATUS_DATA.getCode())) {
			String logType = paramsJson.getString("logType");
			ResultEntity resultEntity = IDataReceivingService.queryLogStatusData(stationCodes, startTime, endTime,
					logType, null, null);
			List<EquipmentInfo> equipmentInfoList = ObjectMapperUtil.convertList(resultEntity, EquipmentInfo.class);
			for (EquipmentInfo equipmentInfo : equipmentInfoList) {
				list.add(MyBeanUtils.convertBeanToMap(equipmentInfo, pattern));
			}
		}
		return ExportUtil.exportExcel(request, list, headerProp, pattern);
	}

}
